﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AttributeRouting.Web.Mvc;
using TemperatureDatabase;

namespace MvcWebRole1.Controllers
{
    [OutputCacheAttribute(VaryByParam = "*", Duration = 0, NoStore = true)]
    public class RegulatorController : Controller
    {
        [POST("Regulator/{id:int}/SetTargetTemperature/{temp:double}/")]
        public string SetTargetTemperature(int id, double temp)
        {
            using (var db = new TemperatureDBEntities())
            {
                var regulator = db.Regulator.SingleOrDefault(x => x.Id == id);
                if (regulator == null)
                    return "Error in id";

                regulator.TargetTemperature = temp;

                db.SaveChanges();
            }

            return "TargetTemperature set to: " + temp;
        }

        [GET("Regulator/{id:int}/GetTargetTemperature/")]
        public string GetTargetTemperature(int id)
        {
            using (var db = new TemperatureDBEntities())
            {
                var regulator = db.Regulator.SingleOrDefault(x => x.Id == id);
                if (regulator == null || !regulator.Active)
                    return "OFF";
                return regulator.TargetTemperature.ToString();
            }
        }

        [GET("Regulator/{id:int}/GetCurrentTemperature/")]
        public string GetCurrentTemperature(int id)
        {
            using (var db = new TemperatureDBEntities())
            {
                var measurement = db.Measurement.Where(x => x.Sensor.Regulator.Id == id)
                    .OrderByDescending(x => x.Timestamp).
                    FirstOrDefault();
                
                if (measurement == null)
                    return "Failed";
                
                return measurement.Temperature.ToString();
            }
        }


        [POST("Regulator/{id:int}/Activate/")]
        public string Activate(int id)
        {
            using (var db = new TemperatureDBEntities())
            {
                var regulator = db.Regulator.SingleOrDefault(x => x.Id == id);
                if (regulator == null)
                    return "Error in id";

                regulator.Active = true;

                db.SaveChanges();
            }

            return "Activated: " + id;
        }

        [POST("Regulator/{id:int}/Deactivate/")]
        public string Deactivate(int id)
        {
            using (var db = new TemperatureDBEntities())
            {
                var regulator = db.Regulator.SingleOrDefault(x => x.Id == id);
                if (regulator == null)
                    return "Error in id";

                regulator.Active = false;

                db.SaveChanges();
            }

            return "Deactivated: " + id;
        }

        [GET("Regulator/Register/{mac}/")]
        public string Register(string mac = "")
        {
            using (var db = new TemperatureDBEntities())
            {
                if (!string.IsNullOrEmpty(mac))
                {
                    var existingRegulator = db.Regulator.SingleOrDefault(x => x.Mac == mac);
                    if (existingRegulator != null)
                    {
                        return existingRegulator.Id.ToString();
                    }
                }

                // Implementation for creation of regulator if system is extended
                
                
                return "false";
            }
        }

    }
}
